<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
	if(isset($_REQUEST['show']) && $_REQUEST['show'] == "buynow"){
		$id = $_REQUEST['id'];
		addIdToCart($id, 1);
	}
	if(isset($_REQUEST['actDel']) && $_REQUEST['actDel'] == "delcart"){
		$id = $_REQUEST['id'];
		remove_product($id);
	}
	if(isset($_REQUEST['btnThanhToan'])){
		checkout();
	}
//add san pham den gio hang
// id san pham - so luong
	function addIdToCart($pid,$q = 1){
		if($pid<1 or $q<1) return;
		if(!isset($_SESSION['cart'])){
			session_register("cart");
		}
		if(is_array($_SESSION['cart'])){
			if(product_exists($pid)) return;
			$max=count($_SESSION['cart']);
			$_SESSION['cart'][$max]['productid']=$pid;
			$_SESSION['cart'][$max]['qty']=$q;
			$_SESSION['cart'][$max]['productname'] = "";
			$_SESSION['cart'][$max]['productprice'] = 0;
		}
		else{
			$_SESSION['cart']=array();
			$_SESSION['cart'][0]['productid']=$pid;
			$_SESSION['cart'][0]['qty']=$q;
			$_SESSION['cart'][0]['productname'] = "";
			$_SESSION['cart'][0]['productprice'] = 0;
		}
	}
	//kiem tra san pham da ron tai
	function product_exists($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		$flag=0;
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				$temp = $_SESSION['cart'][$i]['qty'];
				$_SESSION['cart'][$i]['qty'] = ($temp + 1);
				$flag=1;
				break;
			}
		}
		return $flag;
	}
		//xoa san pham khoi gia hang
	function remove_product($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				unset($_SESSION['cart'][$i]);
				break;
			}
		}
		$_SESSION['cart']=array_values($_SESSION['cart']);
	}
	function string_id(){
		$strId = "";
		if(!isset($_SESSION['cart'])){
			return ;
		}
		$max=count($_SESSION['cart']);
		for($i=0;$i<$max;$i++){
			$strId = $_SESSION['cart'][$i]['productid'] .",";
		}
		$strId =  substr($strId, 0, strlen($strId) - 1);
		return $strId;
	}

	
	function addNameToCard(){
		if(!isset($_SESSION['cart'])){
			return ;
		}
		$_SESSION['cart']=array_values($_SESSION['cart']);
		
		$idList = string_id();
		$sql="select * from sanpham where MaSanPham in (".$idList.")";
		//include("database/DataProvider.php");
		$result = DataProvider::ExecuteQuery($sql);
		
		if(!empty($result))
		{
			$max=count($_SESSION['cart']);
			while ($row=mysql_fetch_array($result)){
				$maSP = $row["MaSanPham"];
				$tenSP = $row["TenSanPham"];
				$giaSanPham = $row["GiaSanPham"];
				
				for($i=0;$i<$max;$i++){
					if($maSP==$_SESSION['cart'][$i]['productid']){
						$_SESSION['cart'][$i]['productname'] = $tenSP;
						$_SESSION['cart'][$i]['productprice'] = $giaSanPham;
						break;
					}
				}
			}
		}
	}
	function checkout(){
		if(!isset($_SESSION['cart'])){
			return ;
		}
		$max=count($_SESSION['cart']);
		if($max <= 0){
			return ;
		}
		$createdate = date("Y-m-d");
		$orderid = createOrderId($createdate);
		$userId = $_SESSION["userid"];
		$totalmoney = totalmoney();
		
		$sql="INSERT INTO dondathang(MaDonDatHang, NgayLap, TongThanhTien, MaTaiKhoan, MaTinhTrang) 
				VALUES ('$orderid','$createdate','$totalmoney', $userId, 1)";
		$result = DataProvider::ExecuteQuery($sql);
		
		for($i=0;$i<$max;$i++){
			$idProduct = $_SESSION['cart'][$i]['productid'];
			$price = $_SESSION['cart'][$i]['productprice'];
			$quatity = $_SESSION['cart'][$i]['qty'];
			$idDetail = createOrderDetailId($i, $orderid);
			
			saveOrderDetail($idDetail, $quatity, $price, $orderid, $idProduct);
		}
		removeAllCart();
	}
	function totalmoney(){
		$max=count($_SESSION['cart']);
		$price = 0;
		for($i=0;$i<$max;$i++){
			$pri = $_SESSION['cart'][$i]['productprice'];
			$qty = $_SESSION['cart'][$i]['qty'];
			$price = $price + ($pri * $qty);
		}
		return $price;
	}
	function createOrderId($date){
		$idor = "";
		$date_arr = explode("-", $date);
		$sql="SELECT * FROM `dondathang` WHERE NgayLap = '$date'";
		$result = DataProvider::ExecuteQuery($sql);
		$numCount= mysql_num_rows($result);
		$idor = substr($date_arr[0], 2, 2) . $date_arr[1] . $date_arr[2] . $numCount;
		return $idor;
	}
	function createOrderDetailId($mun, $orderid){
		$idor1 = "";
		$idor1 = $orderid ."".$mun;
		return $idor1;
	}
	function removeAllCart(){
		unset($_SESSION['cart']);
	}
	function saveOrderDetail($idDetail,$quatity, $price, $idOrder, $idProduct){
		$sql="INSERT INTO chitietdondathang(MaChiTietDonDatHang, SoLuong, GiaBan, MaDonDatHang, MaSanPham)
		VALUES (".$idDetail.",".$quatity.",".$price.",".$idOrder.",".$idProduct.")";
		$result = DataProvider::ExecuteQuery($sql);
		return  $sql;
	}
?>
</body>
</html>
